package com.springboot.controller;

import com.springboot.common.config.Result;
import com.springboot.model.NavigationRecord;
import com.springboot.model.UserLoginForm;
import com.springboot.service.TableBarService;
import com.springboot.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 本控制台用于用户的登录、注册、修改密码
 */
@Controller
public class UserLoginController {

    //声明业务类
    @Autowired
    UserLoginService userLoginService;

    @Autowired
    TableBarService tableBarService;

    //登录界面响应,通过@RequestParam获取参数
    //病人端（用户类型为 1）登录
    @CrossOrigin
    @RequestMapping(value = "/patientlogin")
    public @ResponseBody
    int patientlogin(@RequestParam("phone") String tel,
                     @RequestParam("password") String password){
        System.out.println(tel+" "+password+"正在请求登录");
        try{
            UserLoginForm user = userLoginService.findUserByTel(tel);
            System.out.println("查找到用户"+user.toTestString());
            Integer a = 1;
            if(user.getUserPwd().equals(password)&&user.getUserType().equals(a))
            {
                System.out.println("登录成功");
                Integer userId = user.getUserId();
                NavigationRecord navigationRecord = new NavigationRecord();
                navigationRecord = tableBarService.showTableBarNum(userId);
                return navigationRecord.getTablebarId();
            }
            else {
                return 0;
            }
        }
        catch (Exception e)
        {
            return 0;
        }
    }

    //医生端（用户类型为2）登录和助理端（用户类型为3）登录
    @CrossOrigin
    @RequestMapping(value = "/doctorlogin")
    public @ResponseBody
    Result doctorLogin(@RequestParam("doctorTel") String tel,
                       @RequestParam("doctorPassword") String password,
                       HttpServletRequest request){
        System.out.println(tel+" "+password+"正在请求登录");
        //WEB
        HttpSession session = request.getSession();
        session.setAttribute("tel",tel);
        String str = (String)session.getAttribute("tel");
        System.out.println("session_tel:"+str);
        System.out.println("password:"+password);
//        String result = "False";
        Result result = new Result("False","");
        try{
            UserLoginForm user = userLoginService.findUserByTel(tel);
            System.out.println("成功查找到用户"+user.toString());
            Integer DOCTOR_TYPE = 2;
            Integer ASSISTENT_TYPE =3;
            if(user.getUserPwd().equals(password))
            {
                if(user.getUserType().equals(DOCTOR_TYPE))
                {
                    session.setAttribute("doctorId",user.getUserId());
                    result.setMsg("Doctor");
                    result.setImgUrl(user.getLogoLink());
                }
                else if (user.getUserType().equals(ASSISTENT_TYPE))
                {
                    session.setAttribute("AssistentId",user.getUserId());
                    result.setMsg("Assistent");
                    result.setImgUrl(user.getLogoLink());
                }
            }
        }catch (Exception e)
        {
            result.setMsg("False");
            result.setImgUrl("");
        }
        System.out.println(result);
        return result;
    }

    //管理员登录（用户类型为4）
    @CrossOrigin
    @RequestMapping("/adminlogin")
    @ResponseBody
    public String adminLogin(@RequestBody UserLoginForm userLoginForm){
        System.out.println(userLoginForm.toTestString());
        if(userLoginForm.getUserTel()==null||userLoginForm.getUserTel()==""||userLoginForm.getUserPwd()==null||userLoginForm.getUserPwd()=="")return "kong";
        else if (!userLoginForm.getUserTel().matches("^1[3-9]\\d{9}$")) {
            return "illegal";
        } else{
            try{
                UserLoginForm user = userLoginService.findUserByTel(userLoginForm.getUserTel());
//            System.out.println("查找到用户"+user.toTestString());
                Integer ADMIN = 4;
                if(user.getUserPwd().equals(userLoginForm.getUserPwd())&&user.getUserType().equals(ADMIN))
                {
                    return "yes";
                }
                else {
                    return "no";
                }
            }
            catch (Exception e)
            {
                return "no";
            }
        }

    }
}
